<?php
namespace haibao\cms\business\attendance;

use haibao\cms\business\BusinessException;
use haibao\frame\data\query\Condition;
Class AttendanceDefine extends \haibao\cms\business\Base{
	
	public static function getUserClockTimeByFilter($filter,$isOne = true){
		$data = new \haibao\cms\data\attendance\AttendanceDefine();
		if($isOne){
			return $data->getOne($filter);
		}else{
			return $data->getAll($filter);
		}
	}
	
	public static function addAttendanceRecord($models){
		$data = new \haibao\cms\data\attendance\Attendance();
		if(is_array($models)){
			$data->addAll($models);
		}else{
			$data->add($models);
		}
	}
	
	public static function clearAttendanceRecord($startTime,$endTime){
		
		if(empty($startTime)){
			throw new BusinessException('开始时间不能为空',self::DATA_EMPTY);
		}
		if(empty($endTime)){
			throw new BusinessException('结束时间不能为空',self::DATA_EMPTY);
		}

		$data = new \haibao\cms\data\attendance\Attendance();
		$data->clearAttendanceRecord($startTime,$endTime);
		
	}
	
	public static function updateAttendanceTimeById($id,$time){
		$data = new \haibao\cms\data\attendance\AttendanceDefine();
		$data->updateById($id, $time);
	}
	
	/* public static function deleteAttendanceRecordById($id){
		$data = new \haibao\cms\data\attendance\AttendanceDefine();
		$data->updateById($id, array('Status'=>\haibao\cms\model\data\attendance\AttendanceDefine::STATUS_DELETE));
	} */
	
	public static function removeAttendanceRecord($filter){
		$data = new \haibao\cms\data\attendance\AttendanceDefine();
		$data->remove($filter);
	}
	
	public static function getEmployeeLeave($leaveType = 0){
		
		$employeeLeaveArr = array(
				\haibao\cms\model\data\attendance\AttendanceDefine::ANNUAL_LEAVE => '年假',
				\haibao\cms\model\data\attendance\AttendanceDefine::MARRIAGE_LEAVE => '婚假',
				\haibao\cms\model\data\attendance\AttendanceDefine::FULL_PAY_SICK_LEAVE => '带薪病假',
				\haibao\cms\model\data\attendance\AttendanceDefine::PARTIAL_PAY_SICK_LEAVE => '无薪病假',
				\haibao\cms\model\data\attendance\AttendanceDefine::TIME_IN_LIEU_LEAVE => '补休假',
				\haibao\cms\model\data\attendance\AttendanceDefine::PERSONAL_LEAVE => '事假',
				\haibao\cms\model\data\attendance\AttendanceDefine::MATERNITY_CHECK_LEAVE => '产检假',
				\haibao\cms\model\data\attendance\AttendanceDefine::PATERNITY_LEAVE => '陪产假',
				\haibao\cms\model\data\attendance\AttendanceDefine::MATERNITY_LEAVE => '产假', 
				\haibao\cms\model\data\attendance\AttendanceDefine::NURSING_LEAVE => ' 哺乳假',
				\haibao\cms\model\data\attendance\AttendanceDefine::MISCARRIAGE_LEAVE => '流产休假',
				\haibao\cms\model\data\attendance\AttendanceDefine::FUNERAL_LEAVE => '丧假',
				\haibao\cms\model\data\attendance\AttendanceDefine::BUSINESS_LEAVE => '外出及出差',
				\haibao\cms\model\data\attendance\AttendanceDefine::OTHERS => '其他',
				
		);
		if($leaveType){
			return $employeeLeaveArr[$leaveType];
		}
		return $employeeLeaveArr;
	}
	
	public static function getRecordByJobNumAndClickDate($jobNum,$clickDate){
		if(empty($jobNum)){
			throw new BusinessException('员工工号不能为空',self::DATA_EMPTY);
		}
		if(empty($clickDate)){
			throw new BusinessException('打卡日期不能为空',self::DATA_EMPTY);
		}
		
		$filter = new \haibao\cms\model\filter\attendance\AttendanceDefine();
		$filter->where(\haibao\cms\model\filter\attendance\AttendanceDefine::CONDITION_FIELD_NAME_CLICK_DATE,Condition::CONDITION_EQUAL,$clickDate);
		$filter->where(\haibao\cms\model\filter\attendance\AttendanceDefine::CONDITION_FIELD_NAME_JOB_NUM,Condition::CONDITION_EQUAL,$jobNum);
		$filter->where(\haibao\cms\model\filter\attendance\AttendanceDefine::CONDITION_FIELD_NAME_STATUS,Condition::CONDITION_EQUAL,\haibao\cms\model\data\attendance\AttendanceDefine::STATUS_ENABLE);

		$data = new \haibao\cms\data\attendance\AttendanceDefine();
		return $data->getAll($filter);
	}
	
	public static function delAttendanceRecordById($id){

		self::checkOperation(self::FUNC_CODE_ATTENDANCE_DELETE);
		if(empty($id)){
			throw new BusinessException('考勤不能为空',self::DATA_EMPTY);
		}

		$data = new \haibao\cms\data\attendance\AttendanceDefine();
		$data->removeById($id);
		self::saveOperateHistory(self::FUNC_CODE_ATTENDANCE_DELETE,$id);
	}
	
	public static function delAttendanceRecordByFilter($filter){

		$data = new \haibao\cms\data\attendance\AttendanceDefine();
		$data->remove($filter);
	}
	
	public static function checkClickDateisExist($clickDate,$jobNum){
	    
	    $data = new \haibao\cms\data\attendance\AttendanceDefine();
	    if($data->getAttendanceRecordByClickDate($clickDate,$jobNum)){
	        return true;
	    }else{
	        return false;
	    }
	}
	
	public static function delAttendanceRecordByClickDate($clickDate,$jobNum){
	    
	    $data = new \haibao\cms\data\attendance\AttendanceDefine();
	    $data->delAttendanceRecord($clickDate, $jobNum); 
	}
	
	public static function getLeaveCategory(){
	    $leaveCategoryArr = array(
	        \haibao\cms\model\data\attendance\AttendanceDefine::STATUS_ENABLE => '正常打卡',
	        \haibao\cms\model\data\attendance\AttendanceDefine::MATERNITY_LEAVE => '产假',
	        \haibao\cms\model\data\attendance\AttendanceDefine::NURSING_LEAVE => ' 哺乳假',
	        \haibao\cms\model\data\attendance\AttendanceDefine::MISCARRIAGE_LEAVE => '流产休假'
	    );
	    return $leaveCategoryArr;
	}
	
	public static function checkIsManyClick($jobNum,$clickDate,$startClick = null,$endClick = null){
	    $clickTime = self::getRecordByJobNumAndClickDate($jobNum, $clickDate);
	    $startTimeArr = array();
	    $endTimeArr = array();
	    if(!empty($clickTime)){
	        foreach($clickTime as $list){
                array_push($startTimeArr,$list->StartTime);
                array_push($endTimeArr,$list->EndTime);
	        }
	    }
	    $startTime = date('Gi',strtotime(array_pop($startTimeArr)));
	    $endTime = date('Gi',strtotime(array_pop($endTimeArr)));
	    if($startClick){
	        $startClick = date('Gi',strtotime($startClick));
	        if($startClick - $startTime <10){
	            return true;
	        }else{
	            return false;
	        }
	    }
	    if($endClick){
	        $endClick = date('Gi',strtotime($endClick));
	        if($endClick - $endTime < 10){
	            return true;
	        }else{
	            return false;
	        }
	    }
	}
}